﻿using Mg.Domain;
using Mg.ExpressionParser;
using Mg.ExpressionParser.DbSqlParser;
using Mg.Models.DbModels;
using Mg.Reflection;
//using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleUi
{
    class Program
    {
        static void Main(string[] args)
        {

            string sql = string.Empty;
            SqlParserCore<UserInfo> parser = new SqlParserCore<UserInfo>(new MySQLSqlParser());

            parser.Select().Where(c => c.UserName == "waminglin");

            //parser.Select().Where(c => c.UserName == "waminglin");

            sql = parser.SqlStr;

            Console.WriteLine(sql);
            //Console.ReadKey();

            //var list = GetData(parser);

            //if(list .Any())
            //{

            //}

            //using (MySqlConnection conn = new MySqlConnection("Server=192.168.1.10;userId=root;pwd=123456;database=Ming;"))
            //{
                
            //    Console.WriteLine(sql);
            //    MySqlCommand cmd = new MySqlCommand(sql, conn) { CommandType = System.Data.CommandType.Text };
            //    cmd.Parameters.AddRange(parser.DbDataParams.ToArray());
            //    conn.Open();
            //    MySqlDataReader reader = cmd.ExecuteReader();
            //    while (reader.Read())
            //    {
            //        string UserId = reader["UserId"] == null ? string.Empty : reader["UserId"].ToString();
            //        string UserName = reader["UserName"] == null ? string.Empty : reader["UserName"].ToString();
            //        string Age = reader["Age"] == null ? string.Empty : reader["Age"].ToString();
            //        string Phone = reader["Phone"] == null ? string.Empty : reader["Phone"].ToString();

            //        Console.WriteLine(UserId + ", " + UserName + ", " + Age + ", " + Phone);
            //    }
            //}



            Console.ReadKey();
        }

        //static List<T> GetData<T>(SqlParserCore<T> parser) where T : IEntity
        //{
        //    List<T> result = new List<T>();
        //    using (MySqlConnection conn = new MySqlConnection("Server=192.168.1.10;userId=root;pwd=123456;database=Ming;"))
        //    {
        //        MySqlCommand cmd = new MySqlCommand(parser.SqlStr, conn) { CommandType = System.Data.CommandType.Text };
        //        if (parser.DbDataParams.Any())
        //            cmd.Parameters.AddRange(parser.DbDataParams.ToArray());
        //        conn.Open();
        //        MySqlDataReader reader = cmd.ExecuteReader();
        //        while (reader.Read())
        //        {
        //            T t = Activator.CreateInstance<T>();

        //            foreach (var item in EntityPropertyInfoCollection.GetPropertyInfos(typeof(T)).Where(c => parser.SelectFieldNames.Contains(c.Name)))
        //            {
        //                item.SetValue(t, reader[item.Name]);
        //            }

        //            result.Add(t);
        //        }
        //    }
        //    return result;
        //}


        

    }

}
